<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyCategory extends BaseModel
{
    protected $name = 'sky_category';

    /**
     * 先祖分类
     * @param $category
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list($category){
        $ancestor = (new SkyAncestor())->list($category);
        $where = [
            'category' => $category,
            'status' => 1
        ];
        $list =  $this->field('sub_category as name,cost as total')->where($where)
            ->order('sort desc')
            ->select()->toArray();
        array_walk($list,function(&$item,$key) use ($ancestor){
            $item['spirits'] = !empty($ancestor[$item['name']])?$ancestor[$item['name']]:[];
            $item['total'] = !empty($item['total'])?json_decode($item['total'],true):[];
        });

        return $list;
    }


    /**
     * 季节先祖图标
     * @return array
     */
    public function icon(){
        return $this->where('category','=','季节先祖')
            ->cache(true)
            ->column('icon','sub_category');
    }


    /**
     * 先祖消耗
     * @param $sub_category
     * @return mixed
     */
    public function AncestorCost($sub_category){
        $cost = $this->where('sub_category','=',$sub_category)->cache(true)
            ->value('cost');
        return json_decode($cost,true);
    }
}